﻿using System;
using System.Web.Mvc;
using System.Web.Security;
using Moq;
using NUnit.Framework;
using TeamsPortal;
using TeamsPortal.DataAccess;
using TeamsPortal.Security;
using TeamsPortal.ServiceClient;
using TeamsPortal.Tests.Fakes;
using TeamsPortal.Web.Controllers;

namespace TeamsPortal.Tests.Controllers
{
	[TestFixture]
	public class FormsAuthenticationControllerTest
	{

		[Test]
		public void FormsAuthentication_ResetPassword()
		{
            const string userName = "UserName";
            const string email = "em@email.com";
            var user = new MembershipUser("ClientAuthenticationMembershipProvider", userName, Guid.NewGuid(), email, null, null, true, false, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.MinValue);
            
            var mockMembershipProvider = new Mock<IMembershipService>();
            mockMembershipProvider
                .Setup(m => m.GetUserNameByEmail(email))
                .Returns(userName);
            mockMembershipProvider
                .Setup(m => m.GetUser(userName))
                .Returns(user);

            const int siteUserId = 10;
            var siteUser = new User(siteUserId, userName) { Guid = Guid.NewGuid(), Email = email };
            var mockUsersServiceClient = new Mock<IUsersServiceClient>();
            mockUsersServiceClient
                .Setup(m => m.GetByProviderId(AuthenticationProvider.Membership, user.ProviderUserKey.ToString()))
                .Returns(siteUser);
            mockUsersServiceClient
                .Setup(m => m.UpdatePasswordResetGuid(siteUserId, It.IsAny<string>(), It.IsAny<DateTime>()));

            var controller = new FormsAuthenticationController(null, mockMembershipProvider.Object);
            controller.UsersServiceClient = mockUsersServiceClient.Object;
			controller.ControllerContext = new FakeControllerContext(controller, "http://localhost/forums/");
			controller.Url = new UrlHelper(controller.ControllerContext.RequestContext);

			ActionResult result = controller.ResetPassword(email);

			Assert.IsTrue(controller.ModelState.IsValid);
            Assert.IsInstanceOf<ViewResult>(result);

            mockMembershipProvider.VerifyAll();
            mockUsersServiceClient.VerifyAll();
		}
	}
}
